﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PayexpDAL;

namespace Moneymatters
{
    public partial class LoginForm : Form
    {
        private static string _loggedInUserName = "LogInByPassed";

        public static string LoggedInUserName
        {
            get { return LoginForm._loggedInUserName; }
            set { LoginForm._loggedInUserName = value; }
        }

        private static PayexpThickClientUser _loggedInUser;

        public static PayexpThickClientUser LoggedInUser
        {
            get { return LoginForm._loggedInUser; }
        }

        
        public LoginForm()
        {
            InitializeComponent();
            this.KeyDown += LoginForm_KeyDown;
        }

        void LoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void buttonSignin_Click(object sender, EventArgs e)
        {
            string userName = textBoxUsername.Text.Trim();
            
            string password = textBoxPassword.Text;
            byte[] passwordByteArray = ASCIIEncoding.ASCII.GetBytes(password);
            string encodedPassword = Convert.ToBase64String(passwordByteArray);

            LoginForm._loggedInUser = new DataAccessHandler().IsUserValid(userName, encodedPassword, MainForm.Context);
            if (LoginForm._loggedInUser != null)
            {
                LoginForm._loggedInUserName = LoginForm._loggedInUser.UserName.ToLower();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("The username and/ or the password is incorrect.", "Invalid user", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }
    }
}
